package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.ConnectionLayer;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.editpolicies.ResizableEditPolicy;
import org.eclipse.gef.handles.MoveHandle;
import org.eclipse.gef.handles.NonResizableHandleKit;
import org.eclipse.gef.handles.ResizableHandleKit;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.tools.DragEditPartsTracker;
import org.eclipse.swt.SWT;

import edu.pku.sei.modeler.edit.policies.DiagramLayoutEditPolicy;
import edu.pku.sei.modeler.editpolicies.InteractionLayoutEditPolicy;
import edu.pku.sei.modeler.editpolicies.SequenceNodeEditPolicy;
import edu.pku.sei.modeler.figures.InteractionFigure;
import edu.pku.sei.modeler.figures.InteractionUseFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.uml.BasicInteractions.Interaction;
import edu.pku.sei.modeler.model.uml.Fragments.InteractionUse;
import edu.pku.sei.modeler.model.uml.Kernel.Comment;
import edu.pku.sei.modeler.uml.commands.sequence.SequenceDiagramElementCreateCommandFactory;
import edu.pku.sei.modeler.uml.modelHelper.ModelPackage;

public class InteractionUseEditpart extends AbstractUMLNodeEditPart {

	public InteractionUseEditpart(MetaShapeElement model) {
		super(model);
	}

	protected IFigure createFigure() {
		InteractionUseFigure figure = new InteractionUseFigure();

//		ConnectionLayer connLayer = (ConnectionLayer) getLayer(LayerConstants.CONNECTION_LAYER);
//
//		// connLayer.setConnectionRouter(new HorizontalConnectionRouter());
//		connLayer.setConnectionRouter(null);
//		connLayer.setAntialias(SWT.ON);

		return figure;
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}

	@Override
	protected IFigure getDirectEditFigure() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE, new SequenceNodeEditPolicy());
	}

	public DragTracker getDragTracker(Request request) {
		return new DragEditPartsTracker(this) {
			@Override
			protected void updateTargetRequest() {
				super.updateTargetRequest();
			}
			
			protected Command getCommand() {
				CompoundCommand command = new CompoundCommand();
				Iterator iter = getOperationSet().iterator();
				Request request = getTargetRequest();
				request.setType(REQ_MOVE);
				while (iter.hasNext()) {
					EditPart editPart = (EditPart)iter.next();
					command.add(editPart.getCommand(request));
				}
				return command;
			}
		};
	}
	
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		refreshVisuals();

		super.handlePropertyChanged(event);
	}

	private InteractionUse getModelElement() {
		return (InteractionUse) ((MetaNode) getModel()).getModelElement();
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		InteractionUse modelEle = this.getModelElement();
		InteractionUseFigure figure = (InteractionUseFigure) getFigure();
		figure.setLabel(modelEle.getName());
	}
	
	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connection) {
		return null;
	}
	
	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return null;
	}

	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connection) {
		if (connection.getSource() != null &&
				((MetaNode)connection.getSource().getModel()).getModelElement() instanceof Comment) {
			return super.getTargetConnectionAnchor(connection);
		}
		return null;
	}
	
	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return null;
	}
}
